package com.twentytwok.hospitalsystem22K.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.twentytwok.hospitalsystem22K.common.Code;
import com.twentytwok.hospitalsystem22K.common.R;
import com.twentytwok.hospitalsystem22K.mapper.AdminUserMapper;
import com.twentytwok.hospitalsystem22K.pojo.AdminUser;
import com.twentytwok.hospitalsystem22K.pojo.AdminUserExample;
import com.twentytwok.hospitalsystem22K.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * adminUserService的实现类
 *
 * @author Yuanao
 * @data 2021年11月8日19:55:49
 */
@Service("adminUserService")
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    private AdminUserMapper adminUserMapper;


    @Override
    public AdminUser login(String name, String password) {
        AdminUserExample example = new AdminUserExample();
        example.createCriteria().andNameEqualTo(name).andPasswordEqualTo(password).andDeleteEqualTo(false);

        List<AdminUser> adminUsers = adminUserMapper.selectByExample(example);

        if (adminUsers.size() > 0) {
            return adminUsers.get(0);
        }
        return null;
    }

    @Override
    public R getUsers(Integer page, Integer limit, String name) {


        PageHelper.startPage(page, limit);


        AdminUserExample example = new AdminUserExample();
        AdminUserExample.Criteria criteria = example.createCriteria();
        criteria.andDeleteEqualTo(false);   //必须是没有被删除的

        //当name传过来有值的时候开启模糊查询
        if (name != null) {
            criteria.andNameLike("%" + name + "%"); //开启模糊查询
        }

        List<AdminUser> adminUsers = adminUserMapper.selectByExample(example);

        PageInfo<AdminUser> pageInfo = new PageInfo<>(adminUsers);

        return new R(Code.OK, "OK", (int) pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R deleteUser(Integer id) {
        AdminUser adminUser = new AdminUser();
        adminUser.setId(id);
        adminUser.setDelete(true);
        int result = adminUserMapper.updateByPrimaryKeySelective(adminUser);
        if (result > 0) {
            return new R(Code.OK, "删除成功");
        } else {
            return new R(Code.ERROR, "删除失败");
        }
    }

    @Override
    public R addUser(AdminUser adminUser) {

        int result = adminUserMapper.insertSelective(adminUser);
        if (result > 0) {
            return new R(Code.OK,"添加成功");
        } else {
            return new R(Code.ERROR,"添加失败");
        }
    }

    @Override
    public R getUser(Integer id) {

        AdminUserExample example = new AdminUserExample();
        example.createCriteria().andIdEqualTo(id).andDeleteEqualTo(false);  //获取id等于这个数,并且没有删除的用户

        List<AdminUser> adminUsers = adminUserMapper.selectByExample(example);

        if (!adminUsers.isEmpty()) {
            return new R(Code.OK,"OK",adminUsers.get(0));
        }
        return new R(Code.ERROR,"没有找到指定用户");
    }

    @Override
    public R modifyUser(AdminUser adminUser) {
        //如果传输过来的是空字符串,那么将空字符串转换成null
        if (adminUser.getPassword() != null && adminUser.getPassword().equals("")) {
            adminUser.setPassword(null);
        }

        int result = adminUserMapper.updateByPrimaryKeySelective(adminUser);

        if (result > 0) {
            return new R(Code.OK,"修改成功");
        }
        return new R(Code.ERROR,"修改失败");
    }

    @Override
    public List<AdminUser> getAllAdminUser() {

        AdminUserExample example = new AdminUserExample();
        example.createCriteria().andDeleteEqualTo(false);

        return adminUserMapper.selectByExample(example);
    }
}
